package com.shop.servlet.bclass;

import com.mysql.jdbc.StringUtils;
import com.shop.pajo.BClass;
import com.shop.pajo.Book;
import com.shop.service.bclass.ClassServiceImpl;
import com.shop.service.bclass.IClassService;
import com.shop.service.book.BookServiceImpl;
import com.shop.service.book.IBookService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ClassServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if(method != null && method.equals("query")){
            this.query(req,resp);
        }else if(method != null && method.equals("toAdd")) {
            this.toAdd(req,resp);
        }else if(method != null && method.equals("verification")) {
            this.verification(req,resp);
        }else if(method != null && method.equals("add")) {
            this.add(req,resp);
        }else if(method != null && method.equals("delete")) {
            this.delete(req,resp);
        }

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    public void query(HttpServletRequest req, HttpServletResponse resp){

        String selectChoice = req.getParameter("selectChoice");
        String queryData = req.getParameter("queryData");

        IClassService classService = new ClassServiceImpl();
        IBookService bookService = new BookServiceImpl();
        List<BClass> classList = null;
        Book book = null;

        if(selectChoice != null && selectChoice.equals("bCode")){
            book = bookService.getBookById(queryData);
            classList = classService.query(queryData);
        }

        req.setAttribute("book",book);
        req.setAttribute("classList",classList);

        try {
            req.getRequestDispatcher("/jsp/admin/classAdmin.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    public void toAdd(HttpServletRequest req, HttpServletResponse resp) {
        String bid = req.getParameter("bid");
        if(!StringUtils.isNullOrEmpty(bid)){
            IBookService bookService = new BookServiceImpl();
            Book book = bookService.getBookById(bid);
            req.setAttribute("book",book);
        }
        try {
            req.getRequestDispatcher("/jsp/admin/classAdminAdd.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void verification(HttpServletRequest req, HttpServletResponse resp){
        IBookService bookService = new BookServiceImpl();
        Book book = bookService.getBookById(req.getParameter("bookCode"));
        PrintWriter out = null;
        try {
            out = resp.getWriter();
            if(book != null) {
                out.print("true");
            }else {
                out.print("false");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
        }

    }
    public void add(HttpServletRequest req, HttpServletResponse resp) {
        String bookCode = req.getParameter("bookCode");
        String bookClass = req.getParameter("bookClass");

        IBookService bookService = new BookServiceImpl();
        IClassService classService = new ClassServiceImpl();
        //查询是否存在此书对应的分类
        BClass bClass1 = classService.queryByAll(bookCode, bookClass);
        //如果存在，直接再前端提示
        if(bClass1 != null){
            req.setAttribute("error","添加失败，该分类已经存在");
        }else{
            //不存在则根据id获取书的全部属性
            Book book = bookService.getBookById(bookCode);
            if(book != null){
                BClass bClass = new BClass();
                bClass.setId(bookCode);
                bClass.setbClass(bookClass);
                int addNum = classService.add(bClass);
                if(addNum > 0){
                    req.setAttribute("tips","添加成功");
                }else {
                    req.setAttribute("error","添加失败");
                }
            }else {
                req.setAttribute("error","添加失败，未知的编号！");
            }
        }
        try {
            req.getRequestDispatcher("/jsp/admin/classAdminAdd.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void delete(HttpServletRequest req, HttpServletResponse resp) {
        String bid = req.getParameter("bid");
        String bClass = req.getParameter("bclass");
        IClassService classService = new ClassServiceImpl();
        int deleteNum = classService.delete(bid,bClass);

        if(deleteNum > 0){
            req.setAttribute("tips","删除成功");
        }else {
            req.setAttribute("error","删除失败");
        }

        try {
            req.getRequestDispatcher("/jsp/admin/class.do?method=query&selectChoice=bCode&queryData=" + bid).forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
